var util = require('../../utils/util.js');

Page({
  data: {
    type: '',
    API_BASE_URL: util.API_BASE_URL,
    billid: '',
    evalLevel: 5, //服务态度
    goodsList: [{
      comment: '',
      pics: []
    }],
    xcdData: {}
  },
  //获取行程单预览数据
  getTravelOrder() {
    const _this = this;
    util.request('/itf/common/hongpei/travelorder/getTravelOrder.json', {
      billid: _this.data.billid,
      type: _this.data.type
    }).then(res => {
      if (res.success) {
        _this.setData({
          xcdData: res.data
        })
      } else {
        util.showMsg({ msg: res.message || res.msg });
      }
    });
  },
  //提交评价
  submit() {
    let _this = this;
    const { billid, evalLevel, goodsList, type } = this.data;
    let pj_files = '';
    goodsList[0].pics.forEach(item => {
      if (pj_files.length > 0) {
        pj_files += ',';
      }
      pj_files += item;
    })
    util.request('/itf/common/hongpei/evaluate/addEvaluate.json?type='+type, {
      billid,
      type,
      "pj_star": evalLevel,
      "pj_content": goodsList[0].comment,
      pj_files
    }, 'POST').then(function (res) {
      if (res.success) {
        util.showMsg({
          msg: '评价成功',
          icon: 'success',
          success: () => {
            wx.navigateBack()
          }
        });
      }
    });
  },
  handleScore(e) {
    let aim = e.currentTarget.dataset.aim;
    let evalLevel = e.currentTarget.dataset.evalLevel;
    let obj = {};
    obj[aim] = evalLevel;
    this.setData(obj);
  },
  previewPic(e) {
    let urls = e.currentTarget.dataset.urls;
    wx.previewImage({
      urls
    })
  },
  handleDelete(e) {
    let that = this;
    let src = e.currentTarget.dataset.src;
    let goodsIndex = e.currentTarget.dataset.goodsIndex;
    let pics = e.currentTarget.dataset.pics;
    pics = pics.filter(item => item != src);
    //
    let goodsList = that.data.goodsList;
    goodsList[goodsIndex].pics = pics;
    this.setData({
      goodsList: goodsList
    });
  },
  bindInpuntValue(event) {
    let that = this;
    let goodsIndex = event.target.dataset.goodsIndex;
    let value = event.detail.value;
    let goodsList = that.data.goodsList;
    goodsList[goodsIndex].comment = value
    this.setData({
      goodsList: goodsList
    })
  },
  chooseImageTap: function (e) {
    let that = this;
    let goodsIndex = e.currentTarget.dataset.goodsIndex;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#F32F1D",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album', goodsIndex)
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera', goodsIndex)
          }
        }
      }
    })
  },
  chooseWxImage: function (type, goodsIndex) {
    let _this = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      count: 6,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        let info = wx.getAccountInfoSync()
        for (const key in tempFilePaths) {
          wx.uploadFile({
            url: util.API_BASE_URL + 'itf/common/att/edit.do',
            filePath: tempFilePaths[key],
            name: 'file',
            formData: {
              'dir': 'hongpei',
            },
            header: {
              'maAppId': info.miniProgram.appId || '',
              'content-type': 'multipart/form-data',
              'X-EOA-TOKEN': wx.getStorageSync('X-EOA-TOKEN')
            },
            success: function (res) {
              var picObj = JSON.parse(res.data);
              console.log(picObj)
              let goodsList = _this.data.goodsList;
              goodsList[goodsIndex].pics.push(picObj.id);
              _this.setData({
                goodsList: goodsList
              })
            },
            fail(err) {
              console.error(err);
            }
          })
        }
      }
    })
  },
  onLoad: function (options) {
    this.setData({
      billid: options.billid,
      type: options.type
    });
    this.getTravelOrder();
  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})
